package com.gollum.common.domain.response;


import com.gollum.common.constant.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Map;

/**
 * 通用结果集返回
 *
 * @author Herther
 * @version 1.0.0
 * @ClassName RestResponse.java
 * @createTime 2022年08月07日 12:34:00
 */
@ApiModel(value = "统一响应结果")
public class RestResponse<T> {


    private static final long serialVersionUID = 312991231231620623L;
    /** 成功 */
    public static final Integer SUCCESS = HttpStatus.SUCCESS;

    /** 失败 */
    public static final Integer FAIL = HttpStatus.ERROR;

    @ApiModelProperty(value = "状态码",name = "code")
    private Integer code;

    @ApiModelProperty(value = "消息句柄",name = "msg")
    private String msg;

    @ApiModelProperty(value = "返回的数据",name = "data")
    private T data;

    @ApiModelProperty(value = "拓展字段",name = "extra")
    private Map<String,Object> extra;


    /**
     * 成功相关的
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> success()
    {
        return restResult(null, SUCCESS, null,null);
    }

    public static <T> RestResponse<T> success(T data)
    {
        return restResult(data, SUCCESS, "success",null);
    }

    public static <T> RestResponse<T> success(T data, String msg)
    {
        return restResult(data, SUCCESS, msg,null);
    }

    public static <T> RestResponse<T> success(T data, String msg, Map<String, Object> extra) {
        return restResult(data, SUCCESS, msg,extra);
    }
    /**
     * 失败相关的
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> fail(){
        return restResult(null, FAIL, null, null);
    }

    public static <T> RestResponse<T> fail(String msg)
    {
        return restResult(null, FAIL, msg,null);
    }
    public static <T> RestResponse<T> fail(Integer code ,String msg)
    {
        return restResult(null, code, msg,null);
    }

    public static <T> RestResponse<T> fail(T data)
    {
        return restResult(data, FAIL, "error",null);
    }

    public static <T> RestResponse<T> fail(Map<String,Object> extra)
    {
        return restResult(null, FAIL, null,extra);
    }

    private static <T> RestResponse<T> restResult(T data, int code, String msg,Map<String, Object> extra) {
        RestResponse<T> apiResult = new RestResponse<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setExtra(extra);
        return apiResult;
    }

    public Boolean isSuccess(){
        return SUCCESS.equals(this.code);
    }


    public Integer getCode() {
        return code;
    }

    public RestResponse setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public RestResponse setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public RestResponse setData(T data) {
        this.data = data;
        return this;
    }

    public Map<String, Object> getExtra() {
        return extra;
    }

    public RestResponse setExtra(Map<String, Object> extra) {
        this.extra = extra;
        return this;
    }



    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("code", getCode())
                .append("msg", getMsg())
                .append("data", getData())
                .append("extra", getExtra())
                .append("success", SUCCESS.equals(this.code))
                .toString();
    }

}
